package com.echo.im.platform.controller;


import com.echo.im.common.vo.message.PrivateMessageVO;
import com.echo.im.platform.core.common.Result;
import com.echo.im.platform.core.dto.PrivateMessageDTO;
import com.echo.im.platform.core.dto.PullOfflineMessageDto;
import com.echo.im.platform.core.dto.RecallMessageDTO;
import com.echo.im.platform.service.PrivateMessageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * @author lxy
 * @date 2024-08-14
 */
@Tag(name = "私聊消息")
@RestController
@RequestMapping("/message/private")
@RequiredArgsConstructor
public class PrivateMessageController {

    private final PrivateMessageService privateMessageService;

    @PostMapping("/send")
    @Operation(summary = "发送消息", description = "发送私聊消息")
    public Result<PrivateMessageVO> sendMessage(@Valid @RequestBody PrivateMessageDTO dto) {
        return Result.success(privateMessageService.sendMessage(dto));
    }

    @PostMapping("/recall/{id}")
    @Operation(summary = "撤回消息", description = "撤回私聊消息")
    public Result<Long> recallMessage(@Valid @RequestBody RecallMessageDTO dto) {
        privateMessageService.recallMessage(dto);
        return Result.success();
    }

    @PostMapping("/pullOfflineMessage")
    @Operation(summary = "拉取离线消息", description = "拉取离线消息,消息将通过webscoket异步推送")
    public Result<List<PrivateMessageVO>> pullOfflineMessage(@RequestBody PullOfflineMessageDto dto) throws InterruptedException {
        List<PrivateMessageVO> privateMessages = privateMessageService.pullOfflineMessage(dto);
        return Result.success(privateMessages);
    }

    @PutMapping("/readed")
    @Operation(summary = "消息已读", description = "将会话中接收的消息状态置为已读")
    public Result<Void> readedMessage(@RequestParam Long friendId) {
        privateMessageService.readedMessage(friendId);
        return Result.success();
    }

    @GetMapping("/history")
    @Operation(summary = "查询聊天记录", description = "查询聊天记录")
    public Result<List<PrivateMessageVO>> recallMessage(
            @NotNull(message = "好友id不能为空") @RequestParam Long friendId,
            @NotNull(message = "上次读取最后聊天记录的最后一个的msgId") @RequestParam Long lastMsgId,
            @NotNull(message = "size不能为空") @Size(max = 100, message = "size不能大于100") @RequestParam Integer size) {
        return Result.success(privateMessageService.findHistoryMessage(friendId, lastMsgId, size));
    }

}

